<?php

namespace paper\component;

abstract class Facade
{
    private static mixed $instance = null;

    public static function singleton(string $class, ...$args)
    {
        if (static::$instance == null) {
            if (isset($args[0]) && $args[0] instanceof \Closure) {
                $args = $args[0]();
            }
            static::$instance = (new static())->onCreate($class, ...$args);
        }
        return static::$instance;
    }

    protected static function createNew(string $class, ...$args)
    {
        return (new static())->onCreate($class,...$args);
    }


    protected function onCreate($class, ...$args)
    {
        return new $class(...$args);
    }

    abstract static public function instance(...$args);
}